package com.chy.blog_server.modular.web.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.blog_server.core.util.UserUtil;
import com.chy.blog_server.modular.web.entity.BoMsgSign;
import com.chy.blog_server.modular.web.mapper.BoMsgSignMapper;
import com.chy.blog_server.modular.web.service.IBoMsgSignService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：短信签名表 service业务层
 * 作者：chenhuiyan
 * 时间：2024/08/10
 */
@Service
@Transactional(readOnly = true)
public class BoMsgSignServiceImpl extends ServiceImpl<BoMsgSignMapper, BoMsgSign> implements IBoMsgSignService {

    /**
     * 查询列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> list(Map<String, Object> params){
        return baseMapper.list(params);
    }

    /**
     * 查询总数
     * @param params
     * @return
     */
    public int getCount(Map<String, Object> params){
        return baseMapper.getCount(params);
    }

    /**
     * 查询详情
     * @param id
     * @return
     */
    public Map<String, Object> getInfoById(String id){
        return baseMapper.getInfoById(id);
    }

    /**
     * 保存
     * @param boMsgSign
     * @param request
     * @return
     */
    @Transactional(readOnly = false)
    public Map<String, Object> saveData(BoMsgSign boMsgSign, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        //如果使用状态=使用中，旧记录更新为停用
        if(boMsgSign.getUseStatus() == 0){
            Map<String, Object> params = new HashMap<>();;
            params.put("updateDate", new Date());
            params.put("updateBy", UserUtil.getUserFromSession(request).getId());
            updateUseStatusStop(params);
        }
        if(StrUtil.isEmpty(boMsgSign.getId())){
            boMsgSign.preInsert(request);
            save(boMsgSign);
            res.put("state", "200");
            res.put("message", "添加成功");
        }else{
            boMsgSign.preUpdate(request);
            updateById(boMsgSign);
            res.put("state", "200");
            res.put("message", "修改成功");
        }
        return res;
    }

    /**
     * 更新旧记录使用状态为停用
     * @return
     */
    public int updateUseStatusStop(Map<String, Object> params){
        return baseMapper.updateUseStatusStop(params);
    }

    /**
     * 查询已删除列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> recycleList(Map<String, Object> params){
        return baseMapper.recycleList(params);
    }

    /**
     * 彻底删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int thoroughlyDelete(String id){
        return baseMapper.thoroughlyDelete(id);
    }

}
